#include "displayer.h"

Displayer::Displayer(int videoMode, QWidget *parent) : QWidget(parent)
{
    if(videoMode == 0) {
        m_currentLayout = 1;
    } else if(videoMode == 1) {
        m_currentLayout = 2;
    } else if(videoMode == 2) {
        m_currentLayout = 4;
    }
    setWindowIcon(QIcon(":/img/videologo.png"));
    m_gridLayout = new QGridLayout(this);
}

Displayer::~Displayer()
{
    m_gridLayout->deleteLater();
    for(auto it: m_videoWindows) {
        delete it;
    }
}

bool Displayer::startReceiving()
{
    for(int i = 0; i < m_videoWindows.size(); i++) {
        m_videoWindows[i]->startReceiving();
    }
    return true;
}

void Displayer::resetSettings(int videoID)
{
    if(m_videoWindows.size() > videoID) {
         m_videoWindows[videoID]->resetSettings(m_windowConfig[videoID].ip, m_windowConfig[videoID].port,
                                                m_windowConfig[videoID].netCard, m_windowConfig[videoID].bindWay);
    }
}

void Displayer::initUI()
{
    if(m_videoWindows.size() >= 4) return;
    //循环初始化四个显示控件
    for(int i = 0; i < 4; i++) {
       m_videoWindows.append(new VideoWindow(m_windowConfig[i].resourcesName, m_windowConfig[i].netCard,
                                             m_windowConfig[i].ip, m_windowConfig[i].port, m_windowConfig[i].bindWay)); //loopback_0 ethernet_32769
    }
    //连接转存，但要分1 - 4屏幕,转给mainWindow中的MainControl类去存储
    for(int i = 0; i < m_videoWindows.size(); i++) {
        connect(m_videoWindows[i], &VideoWindow::sigSendDataToSave, [=](const QByteArray &data, QString ipSrc, quint16 portSrc){
            emit sigSendDataToSave(i, data, ipSrc, portSrc);
        });
        //到设置界面显示ip源
        connect(m_videoWindows[i], &VideoWindow::sigSrcIP, [=](QString src){
            emit sigSrcs(i, src);
        });

        connect(m_videoWindows[i], &VideoWindow::sigVideoInfoChanged, [=](int width, int height, int fps){
            emit sigVideoInfoChanged(i, width, height, fps);
        });

    }
    onChangeViewMode(m_currentLayout);
}

void Displayer::onChangeViewMode(int mode)
{
    //更新变量
    //if(mode == m_currentLayout) return;

    m_currentLayout = mode;

    // 从布局中移除所有窗口
    for(auto window : m_videoWindows) {
        m_gridLayout->removeWidget(window);
        window->hide();
    }

    emit sigChangedVideoMode(mode);

    //根据mode的值重新布局
    switch (mode) {
        case 1: {
            m_gridLayout->addWidget(m_videoWindows[0], 0, 0, 1, 1);
            m_videoWindows[0]->show();
            break;
            //    m_gridLayout.setContentsMargins(5, 5, 5, 5);
            //    m_gridLayout.setRowStretch(0, 1);  // 第 1 行拉伸权重
            //    m_gridLayout.setRowStretch(1, 1);  // 第 2 行拉伸权重
            //    m_gridLayout.setColumnStretch(0, 1);  // 第 1 列拉伸权重
            //    m_gridLayout.setColumnStretch(1, 1);  // 第 2 列拉伸权重
        }
        case 2: {
            m_gridLayout->addWidget(m_videoWindows[0], 0, 0, 1, 1);
            m_gridLayout->addWidget(m_videoWindows[1], 0, 1, 1, 1);
            m_videoWindows[0]->show();
            m_videoWindows[1]->show();
            break;
        }
        case 4: {
            m_gridLayout->addWidget(m_videoWindows[0], 0, 0, 1, 1);
            m_gridLayout->addWidget(m_videoWindows[1], 0, 1, 1, 1);
            m_gridLayout->addWidget(m_videoWindows[2], 1, 0, 1, 1);
            m_gridLayout->addWidget(m_videoWindows[3], 1, 1, 1, 1);
            for(auto video : m_videoWindows) {
                video->show();
            }
            break;
        }
        default:
            break;
    }
}

void Displayer::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(this);
    m_loadQss.loadStyleSheet(&menu, ":/qss/QMenu_2.qss");

    QAction *fullScreenShow = menu.addAction("全屏显示");
    QAction *maxScreenShow = menu.addAction("正常显示");
    menu.addSeparator();
    //QAction *currentDisplayName = menu.addAction("显示当前显示器名称");
    QAction *displayMode = menu.addAction("显示模式设置");
    //QAction *importVideo = menu.addAction("视频导出");
    menu.addSeparator();
    QAction *settings = menu.addAction("设置菜单");
    menu.addSeparator();
    QAction *exit = menu.addAction("退出整个程序");

    //显示模式设置添加子菜单
    QMenu displayModeSubMenu(this);
    m_loadQss.loadStyleSheet(&displayModeSubMenu, ":/qss/QMenu_2.qss");

    QAction *singleView = displayModeSubMenu.addAction("单视图");
    QAction *twoView = displayModeSubMenu.addAction("双视图");
    QAction *fourView = displayModeSubMenu.addAction("四视图");
    displayMode->setMenu(&displayModeSubMenu);

    QAction *selectedAction = menu.exec(event->globalPos());

    //处理选择
    if (selectedAction == singleView) {
        onChangeViewMode(1);
    } else if (selectedAction == twoView) {
        onChangeViewMode(2);
    } else if (selectedAction == fourView) {
        onChangeViewMode(4);
    } else if(selectedAction == settings) {
        emit sigOpenSettings();
    } else if(selectedAction == exit) {
        emit sigExit();
    } else if(selectedAction == fullScreenShow) {
        this->showFullScreen();
    } else if(selectedAction == maxScreenShow) {
        this->showNormal();
        this->showMaximized();
    }
    /*else if(selectedAction == currentDisplayName) {
        QScreen *currentScreen = nullptr;
        QPoint globalPos = mapToGlobal(QPoint(0, 0)); //获取控件(0,0)坐标在全局坐标中的位置
        currentScreen = QApplication::screenAt(globalPos);
        Toast::showMessage(this, currentScreen->name(), 2000); // 1秒后自动消失
    }*/
}


